﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using AmazonDynamoDBStub.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AmazonDynamoDBStub.Extensions
{
    [TestClass]
    public class PrimitiveExtensionsTest
    {
        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ShouldThrowNotSupportedExceptionIfLeftTypeIsInvalid()
        {
            Primitive left = new Primitive("1", false);
            Primitive right = new Primitive("2", true);

            Primitive result = left.Add(right);
        }

        [TestMethod]
        [ExpectedException(typeof(NotSupportedException))]
        public void ShouldThrowNotSupportedExceptionIfRightTypeIsInvalid()
        {
            Primitive left = new Primitive("1", true);
            Primitive right = new Primitive("2", false);

            Primitive result = left.Add(right);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidCastException))]
        public void ShouldThrowInvalidCastExceptionIfLeftValueIsInvalid()
        {
            Primitive left = new Primitive("str", true);
            Primitive right = new Primitive("1", true);

            Primitive result = left.Add(right);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidCastException))]
        public void ShouldThrowInvalidCastExceptionIfResultExceedingTheValueRange()
        {
            Primitive left = new Primitive("30000", true);
            Primitive right = new Primitive("2768", true);

            Primitive result = left.Add(right);
            Int16 valueOfResult = result.AsShort();
        }

        [TestMethod]
        public void ShouldGetCorrectResultAfterAdditionForInt16()
        {
            Primitive left = new Primitive("30000", true);
            Primitive right = new Primitive("2767", true);

            Primitive result = left.Add(right);

            Assert.AreEqual(Int16.MaxValue, result.AsShort());
        }

        [TestMethod]
        public void ShouldGetCorrectResultAfterAdditionForInt32()
        {
            Primitive left = new Primitive("2000000000", true);
            Primitive right = new Primitive("147483647", true);

            Primitive result = left.Add(right);

            Assert.AreEqual(Int32.MaxValue, result.AsInt());
        }

        [TestMethod]
        public void ShouldGetCorrectResultAfterAdditionForInt64()
        {
            Primitive left = new Primitive("9000000000000000000", true);
            Primitive right = new Primitive("223372036854775807", true);

            Primitive result = left.Add(right);

            Assert.AreEqual(Int64.MaxValue, result.AsLong());
        }

        [TestMethod]
        public void ShouldGetCorrectResultAfterAdditionForDouble()
        {
            Primitive left = new Primitive("1e+100", true);
            Primitive right = new Primitive("-1e+100", true);

            Primitive result = left.Add(right);

            Assert.AreEqual(0, result.AsDouble());
        }
    }
}
